<?php

namespace paper\component;

use paper\http\Request;
use paper\http\response\RedirectResponse;
use paper\http\response\Response;
use paper\http\response\ViewResponse;

abstract class Controller implements \paper\constraint\Controller
{
    private array $assign = [];
    private Request $request;

    function onBefore(Request $request)
    {
        $this->request = $request;
    }

    function onAfter(Response $response)
    {
        // TODO: Implement onAfter() method.
    }

    function assign($name, $value)
    {
        $this->assign[$name] = $value;
    }

    function redirect($url):RedirectResponse
    {
        return new RedirectResponse($url);
    }

    function view(string $template, $vars = []): ViewResponse
    {
        $vars = array_merge($this->assign, $vars);
        return (new ViewResponse($template,200))->setVars($vars);
    }
    private function toUnderScore($camelCaps, $separator = '_'): string
    {
        return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
    }

}